package com.whu.dbpedia;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;

import com.whu.graph.util.SequentialIntClassArray;

public class ProcessDbpedia {

	private String prefix = "/home/sanglei/vbox_share/DBpedia数据/处理过的数据";

	private String typePath = prefix + "/source_instance_types_en.nt";
	private String typeReturnPath = prefix + "/temp_instance_types_op.nt";

	private String typePath2 = typeReturnPath;
	private String typeReturnPathClear = prefix
			+ "/result_insance2class_types_op.nt";

	private String typeReturnPathReverse = prefix
			+ "/result_class2instance_types_op.nt";

	private String typeReturnPathOfClassOnly = prefix
			+ "/result_class_only_types.nt";

	private String typeReturnPathOfRelationOnly = prefix
	+ "/result_relation_only_types.nt";
	
	private String proPath = prefix + "/source_mappingbased_properties_en.nt";
	private String proReturnPath = prefix
			+ "/temp_rel_mappingbased_properties_en.nt";
	private String proReturnPath2 = prefix
			+ "/temp_val_mappingbased_properties_en.nt";

	public void getInstanceAndClass() {
		BufferedReader br = null;
		FileWriter fw = null;
		String line = "";
		StringBuffer sb = new StringBuffer();

		try {
			fw = new FileWriter(typeReturnPath);
			br = new BufferedReader(new FileReader(new File(typePath)));
			while (br.ready()) {
				line = br.readLine().trim();
				String[] splits = line.split(" ");

				for (int m = 0; m < splits.length; m++) {
					if (-1 != splits[m].indexOf("dbpedia")) {
						int i = splits[m].lastIndexOf("/");
						splits[m] = splits[m].substring(i + 1,
								splits[m].length() - 1);
					} else {
						splits[m] = "";
					}
				}

				if (!splits[0].equals("") && !splits[2].equals("")) {
					sb.append(splits[0] + "\t" + splits[2] + "\n");
					if (sb.length() > 100000) {
						fw.write(sb.toString());
						sb = new StringBuffer();
					}
				}
			}

			fw.write(sb.toString());
			fw.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void getInstanceAndClassSimply() {
		BufferedReader br = null;
		FileWriter fw = null;
		String line = "";
		StringBuffer sb = new StringBuffer();
		int count = 0;

		try {
			fw = new FileWriter(typeReturnPathClear);
			br = new BufferedReader(new FileReader(new File(typePath2)));
			String former = "";
			String tmp = "";
			if (br.ready()) {
				line = br.readLine().trim();
				former = line.split("\t")[0];
				tmp = line;
			}
			while (br.ready()) {
				line = br.readLine().trim();

				String[] splits = line.trim().split("\t");
				if(splits[1].equals("PersonFunction")){
					continue;
				}
				
				if (splits[0].equals(former)) {
					tmp += "/" + splits[1];
				} else {// new start
					sb.append(tmp + "\n");
					count++;
					former = splits[0];
					tmp = line;
				}

				if (sb.length() > 100000) {
					fw.write(sb.toString());
					sb = new StringBuffer();
				}
			}

			fw.write(sb.toString());
			fw.close();
			System.out.println("numbers of instances :" + count);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void getInstanceAndClassReverse() {
		BufferedReader br = null;
		FileWriter fw = null;
		String line = "";
		StringBuffer sb;

		HashMap<String, StringBuffer> store = new HashMap<String, StringBuffer>();

		try {
			fw = new FileWriter(typeReturnPathReverse);
			br = new BufferedReader(new FileReader(new File(typePath2)));

			while (br.ready()) {
				line = br.readLine().trim();
				String[] splits = line.trim().split("\t");

				if (store.containsKey(splits[1])) {
					sb = store.get(splits[1]);
					sb.append(splits[0] + "/");
				} else {
					sb = new StringBuffer(splits[0] + "/");
				}

				store.put(splits[1], sb);
			}

			for (String key : store.keySet()) {
				sb = store.get(key);
				fw.write(sb.toString() + "\n");
			}

			fw.close();
			System.out.println("numbers of class :" + store.size());

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void getAllClass() {
		BufferedReader br = null;
		FileWriter fw = null;
		String line = "";

		HashMap<String,Integer> store = new HashMap<String,Integer>();
		SequentialIntClassArray sortedStore = null;

		try {
			fw = new FileWriter(typeReturnPathOfClassOnly);
			br = new BufferedReader(new FileReader(new File(typePath2)));

			while (br.ready()) {
				line = br.readLine().trim();
				String[] splits = line.trim().split("\t");

				if (!store.containsKey(splits[1])) {					
					store.put(splits[1],1);
				}else{
					int i = store.get(splits[1]);
					store.put(splits[1], i+1);
				}
			}
			
			sortedStore = new SequentialIntClassArray(store.size());
			
			for(String key : store.keySet()){
				sortedStore.insert(store.get(key).intValue(), key);				
			}
			
			//打印
			String []classes = sortedStore.getAllClasses();
			int [] score = sortedStore.getArray();
			
			for(int i=0;i<score.length;i++){
				fw.write(classes[i]+" "+score[i]+"\n");
			}

			fw.close();
			System.out.println("numbers of class :" + store.size());

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void getAllRel(){
		BufferedReader br = null;
		FileWriter fw = null;
		String line = "";
		int count=0;

		HashMap<String,Integer> store = new HashMap<String,Integer>();
		SequentialIntClassArray sortedStore = null;

		try {
			fw = new FileWriter(typeReturnPathOfRelationOnly);
			br = new BufferedReader(new FileReader(new File(proReturnPath)));

			while (br.ready()) {
				line = br.readLine().trim();
				String[] splits = line.trim().split("\t");
				
				count++;
				
				if(splits[1].equals("2007")){
					System.out.println("count:"+count+","+line);
					break;
				}

				if (!store.containsKey(splits[1])) {					
					store.put(splits[1],1);
				}else{
					int i = store.get(splits[1]);
					store.put(splits[1], i+1);
				}
			}
			
			sortedStore = new SequentialIntClassArray(store.size());
			
			for(String key : store.keySet()){
				sortedStore.insert(store.get(key).intValue(), key);				
			}
			
			//打印
			String []classes = sortedStore.getAllClasses();
			int [] score = sortedStore.getArray();
			
			for(int i=0;i<score.length;i++){
				fw.write(classes[i]+" "+score[i]+"\n");
			}

			fw.close();
//			System.out.println("numbers of class :" + store.size());

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void getInstanceAndRelation() {
		BufferedReader br = null;
		FileWriter fw_relation = null;
		FileWriter fw_value = null;
		String line = "";
		StringBuffer sb_value = new StringBuffer();
		StringBuffer sb_relation = new StringBuffer();
		try {
			fw_relation = new FileWriter(proReturnPath);
			fw_value = new FileWriter(proReturnPath2);
			br = new BufferedReader(new FileReader(new File(proPath)));

			int flag = 0;
			while (br.ready()) {
				line = br.readLine().trim();
				String[] splits = line.split(">");
				String tmp = "";
				if (splits.length >= 3) {// 至少有3个
					// 第一个split是instance
					tmp = splits[0].trim();
					if (tmp.indexOf("dbpedia") != -1) {
						int end = tmp.lastIndexOf("/");
						tmp = tmp.substring(end + 1, tmp.length());
						if(tmp.equals("")){
							continue;
						}else{
							splits[0]  = tmp;
						}
					} else {
						continue;
					}

					// 第2个split是relation or property
					tmp = splits[1].trim();
					if (tmp.indexOf("dbpedia") != -1) {
						int end = tmp.lastIndexOf("/");
						tmp = tmp.substring(end + 1, tmp.length());
						if(tmp.equals("")){
							System.out.println(line);
							continue;
						}else{
							splits[1]  = tmp;
						}
					} else {
						continue;
					}

					// 第3个是 value or instance
					tmp = splits[2].trim();
					if (tmp.startsWith("\"")) {// 值
						int m = tmp.lastIndexOf("\"");
						if (m == 0) {
							// m=0;
							continue;
						}
						splits[2] = tmp.substring(1, m);
						flag = 1;
					} else if (tmp.startsWith("<")) {// instance
						int end = tmp.lastIndexOf("/");
						splits[2] = tmp.substring(end + 1, tmp.length());
						flag = 2;
					} else {
						continue;
					}
				}

				if (flag == 1) {
					sb_value.append(splits[0] + "\t" + splits[1] + "\t"
							+ splits[2] + "\n");
					if (sb_value.length() > 30000000) {
						fw_value.write(sb_value.toString());
						sb_value = new StringBuffer();
						System.out.println("sb_value out of mem,so i'm writting it to disk!");
					}
				} else if (flag == 2) {
					sb_relation.append(splits[0] + "\t" + splits[1] + "\t"
							+ splits[2] + "\n");
					if (sb_relation.length() > 30000000) {
						fw_relation.write(sb_relation.toString());
						sb_relation = new StringBuffer();
						System.out.println("sb_relation out of mem,so i'm writting it to disk!");
					}
				}

				flag = 0;
			}

			fw_value.write(sb_value.toString());
			fw_relation.write(sb_relation.toString());
			fw_relation.close();
			fw_value.close();
			System.out.println("finished!");

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ProcessDbpedia pd = new ProcessDbpedia();
		// pd.getInstanceAndClass();
//		 pd.getInstanceAndClassSimply();
//		 pd.getInstanceAndClassReverse();
//		pd.getInstanceAndRelation();
//		pd.getAllClass();
//		pd.getAllRel();
	}

}
